﻿using System;
using System.Collections.Generic;
using Evaluant.NLinq.Expressions;

namespace Evaluant.NLinq.Expressions
{
    public class BlockExpression : Expression, IList<Expression>
    {
        public BlockExpression()
        {
            expressions = new List<Expression>();
        }

        public BlockExpression(Expression expression)
            : this()
        {
            expressions.Add(expression);
        }

        private IList<Expression> expressions;

        #region IList<Expression> Members

        public int IndexOf(Expression item)
        {
            return expressions.IndexOf(item);
        }

        public void Insert(int index, Expression item)
        {
            expressions.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            expressions.RemoveAt(index);
        }

        public Expression this[int index]
        {
            get
            {
                return expressions[index];
            }
            set
            {
                expressions[index] = value;
            }
        }

        #endregion

        #region ICollection<Expression> Members

        public void Add(Expression item)
        {
            this.expressions.Add(item);
        }

        public void Clear()
        {
            expressions.Clear();
        }

        public bool Contains(Expression item)
        {
            return expressions.Contains(item);
        }

        public void CopyTo(Expression[] array, int arrayIndex)
        {
            expressions.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return expressions.Count; }
        }

        public bool IsReadOnly
        {
            get { return expressions.IsReadOnly; }
        }

        public bool Remove(Expression item)
        {
            return expressions.Remove(item);
        }

        #endregion

        #region IEnumerable<Expression> Members

        public IEnumerator<Expression> GetEnumerator()
        {
            return expressions.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return expressions.GetEnumerator();
        }

        #endregion

        public override void Accept(NLinqVisitor visitor)
        {
            foreach (var expression in this)
            {
                expression.Accept(visitor);
            }
        }

        public override ExpressionTypes ExpressionType
        {
            get { return ExpressionTypes.Block; }
        }

        public Expression[] ToArray()
        {
            return ((List<Expression>)expressions).ToArray();
        }
    }
}
